package com.yumis.service.modules.shop.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.exception.GlobalException;
import com.yumis.service.common.vo.Page;
import com.yumis.service.common.vo.Response;
import com.yumis.service.model.pojo.Shop;
import com.yumis.service.model.pojo.ShopVipCard;
import com.yumis.service.model.pojo.User;
import com.yumis.service.modules.shop.mapper.ShopVipCardMapper;
import com.yumis.service.modules.shop.service.ShopVipCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ShopVipCardServiceImpl implements ShopVipCardService {
    @Resource
    private ShopVipCardMapper shopVipCardMapper;
    @Override
    public Page<ShopVipCard> queryShopVipCard(Integer page, Integer rows) {
        //分页
        PageHelper.startPage(page,rows);
        //过滤
        Example example = new Example(ShopVipCard.class);
        example.setOrderByClause("id desc");
        List<ShopVipCard> shopVipCardList=shopVipCardMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(shopVipCardList)){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        PageInfo<ShopVipCard> info = new PageInfo<>(shopVipCardList);
        return new Page<>(info.getTotal(),info.getPages(),shopVipCardList);
    }

    @Override
    public void addShopVipCard(ShopVipCard shopVipCard) {
        int i=shopVipCardMapper.insert(shopVipCard);
        if (i!=1){
            throw new GlobalException(Response.buildResponse(HttpState.CREATED_FAILED));
        }
    }

    @Override
    public void deleteShopVipCardById(Long id) {
        int i=shopVipCardMapper.deleteByPrimaryKey(id);
        if (i !=1){
            throw new GlobalException(Response.buildResponse(HttpState.DELETE_ERROR));
        }
    }

    @Override
    public ShopVipCard queryShopVipCardById(Long id) {
        ShopVipCard shopVipCard=shopVipCardMapper.selectByPrimaryKey(id);
        if (shopVipCard ==null || shopVipCard.equals("") ){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        return shopVipCard;
    }

    @Override
    public void updateShopVipCard(ShopVipCard shopVipCard) {
        int i=shopVipCardMapper.updateByPrimaryKeySelective(shopVipCard);
        if (i !=1){
            throw new GlobalException(Response.buildResponse(HttpState.UPDATE_ERROR));
        }
    }
}
